package com.wzg.appframe.web.local;

import android.util.Log;

import com.wzg.appframe.utils.FileUtil;
import com.wzg.appframe.utils.TLog;

import java.io.File;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class LocalResInit {
    public static final String FILE_DIR_NAME = "2019mifu";
    public static final String H5_DIR_NAME = "H5";
    public static final String Log_DIR_NAME = "LOG";

    public static String h5Path, resPath, logPath;

    public static void initResDir() {
        String sdPath = FileUtil.getSdCardPath();
        resPath = sdPath + "/" + FILE_DIR_NAME;
        h5Path = resPath + "/" + H5_DIR_NAME;
        logPath = resPath + "/" + Log_DIR_NAME;
        File resFile = new File(resPath);
        File h5File = new File(h5Path);
        File logFile = new File(logPath);
        if (!resFile.exists()) {
            boolean f = resFile.mkdirs();
        }
        if (!h5File.exists()) {
            h5File.mkdirs();
        }
        if (!logFile.exists()) {
            logFile.mkdirs();
        }

        new Thread(new Runnable() {
            @Override
            public void run() {
                clearLocalLogs();
            }
        }).start();
    }

    //清理30天前的日志
    static void clearLocalLogs() {
        File f = new File(logPath);
        File[] files = f.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.exists() && file.isFile()) {
                    String FileName = file.getName().substring(0, file.getName().indexOf("."));
                    String todayName = convertDate(new Date(), "yyyy-MM-dd");
                    Date fileDate = convertString2Date(FileName, "yyyy-MM-dd");
                    Date todayDate = convertString2Date(todayName, "yyyy-MM-dd");
                    if (fileDate != null && todayDate != null) {
                        if (todayDate.getTime() - fileDate.getTime() >= 86400000 * 30) {
                            file.delete();
                        }
                    }
                }
            }
        }

    }

    static String convertDate(Date date, String format) {
        if (date != null) {
            DateFormat format1 = new SimpleDateFormat(format, Locale.ENGLISH);
            return format1.format(date);
        } else {
            return "";
        }
    }

    static Date convertString2Date(String str, String formatStr) {
        DateFormat format = new SimpleDateFormat(formatStr);
        Date date = null;

        try {
            date = format.parse(str);
        } catch (ParseException var5) {
            var5.printStackTrace();
        }

        return date;
    }

    public static boolean isNeedInitRes() {
        File h5File = new File(h5Path);

        return !h5File.exists();
    }

    public String getH5Path() {
        return h5Path;
    }

    public String getLogPaht() {
        return logPath;
    }
}
